﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraTreeList;
using XCI.Windows;

namespace XCI.Setting.Controls
{
    public partial class SettingTreeGeneral : BaseUserControl
    {
        private readonly Dictionary<string, Control> optionControls = new Dictionary<string, Control>();
        private TreeList treeList;
        private Control currentOptionControl;
        public SettingTreeGeneral()
        {
            InitializeComponent();
        }

        public void Init(TreeList treeList)
        {
            this.treeList = treeList;
            this.accordion.SelectedElement = this.elementOptionsView;
        }

        private void SettingGridGeneral_Load(object sender, EventArgs e)
        {
            UpdateCurrentOptionsVisibility();
        }

        private void Accordion_SelectedElementChanged(object sender, DevExpress.XtraBars.Navigation.SelectedElementChangedEventArgs e)
        {
            UpdateCurrentOptionsVisibility();
        }

        void UpdateCurrentOptionsVisibility()
        {
            if (accordion.SelectedElement?.Tag == null) return;
            var name = accordion.SelectedElement.Tag.ToString();
            if (currentOptionControl != null) currentOptionControl.Visible = false;
            if (!optionControls.TryGetValue(name, out var targetOptionControl))
            {
                targetOptionControl = CreateOptionControl(name);
                if (targetOptionControl == null) return;
                targetOptionControl.Dock = DockStyle.Fill;
                panelOption.Controls.Add(targetOptionControl);
                optionControls[name] = targetOptionControl;
            }
            currentOptionControl = targetOptionControl;
            if (targetOptionControl != null) targetOptionControl.Visible = true;
        }

        Control CreateOptionControl(string name)
        {
            switch (name)
            {
                case "OptionsView"://显示
                    return new SettingTreeOptionsView(treeList);
                case "OptionsBehavior"://功能
                    return new SettingTreeOptionsBehavior(treeList);
                case "OptionsSelection"://选择
                    return new SettingTreeOptionsSelection(treeList);
                case "OptionsFind"://查找
                    return new SettingTreeOptionsFind(treeList);
            }
            return null;
        }
    }
}
